package com.pmcc.core.service;

import com.pmcc.core.entity.security.Dict;
import com.pmcc.core.repository.DictRepository;
import com.pmcc.utils.BeanUtils;
import com.pmcc.utils.AntdPage;
import com.pmcc.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pmcc.utils.ResultBean;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by chenqf on 2017/12/14.
 */
@Service
@Transactional
public class DictService {
    @Autowired
    DictRepository dictRepository;

    /**
     * 查询列表
     *
     * @return
     */
    public ResultBean queryList(AntdPage antdPage, Dict dict) {
        ResultBean resultBean=new ResultBean(ResultBean.SUCCESS);
        org.springframework.data.domain.Page dictPage=dictRepository.findAll(this.getSpecification(dict), antdPage.getPageRequest());
        resultBean.setPage(antdPage.getDefaultCurrent());
        resultBean.setTotal(dictPage.getTotalElements());
        resultBean.setPageSize(antdPage.getPageSize());
        resultBean.setRows(dictPage.getContent());
        return resultBean;
    }

    public Specification<Dict> getSpecification(Dict dict) {
        return new Specification<Dict>() {
            @Override
            public Predicate toPredicate(Root<Dict> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(builder.like(root.get("parentId").as(String.class),  "%"+dict.getParentId().trim()+"%"));
                //编码
                if (StringUtil.isNotEmpty(dict.getCode())) {
                    list.add(builder.like(root.get("code").as(String.class),  "%"+dict.getCode().trim()+"%"));
                }
                //名称
                if(StringUtil.isNotEmpty(dict.getName())){
                    list.add(builder.like(root.get("name").as(String.class),  "%"+dict.getName().trim()+"%"));
                }
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }

    /**
     * 保存明细
     * @param parentId
     * @param list
     */
    public void saveDetail(String parentId,List<Map<String,String>> list){
        for (Map<String,String> map : list){
            if("0".equals(map.get("delFlag"))) {
                Dict dict;
                if (StringUtil.isEmpty(map.get("id"))) {
                    dict = new Dict();
                } else {
                    dict = dictRepository.findById(map.get("id"));
                }
                BeanUtils.setBeanData(dict,map);
                dict.setParentId(parentId);
                dict.setSort(1);
                dict.setStatus("Y");
                dictRepository.save(dict);
            }else{
                dictRepository.delete(map.get("id"));
            }
        }
    }
    /**
     * 删除
     *
     * @param id
     * @return
     */
    public ResultBean delete(String id) {
        ResultBean resultBean = new ResultBean();
        dictRepository.deleteByParentId(id);//删除子节点
        dictRepository.delete(id);//删除自己
        resultBean.setResultCode(ResultBean.SUCCESS);
        return resultBean;
    }


    /**
     * 查询下拉框
     * @param code 字典类型编码
     * @return
     */
    public List<Dict> queryToCombo(String code) {
        List<Dict> pList = dictRepository.findByCode(code);
        if (pList.size()>0){
            List<Dict> dictList = dictRepository.findByParentIdAndDelFlagOrderBySortAsc(pList.get(0).getId(), "0");
            return dictList;
        }
        return null;
    }


    /**
     * 根据类型和值 转name
     *
     * @param code 字典类型code
     * @param value 字典值
     * @return
     */
    public String queryDictName(String code, String value) {
        List<Dict> pList = dictRepository.findByCode(code);
        if (pList.size()>0){
            List<Dict> list = dictRepository.findByParentIdAndCodeAndDelFlag(pList.get(0).getId(), value, "0");
            if (list != null && list.size() > 0) {
                return list.get(0).getName();
            } else {
                return "";
            }
        }
        return "";
    }
}
